<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\admin\model\Team;
use app\common\model\Authentication;
use app\common\model\CaptainApplication;
use app\common\model\User;
use app\common\model\UserFollow;
use think\Db;
use think\Exception;

class FollowManager extends BaseManager
{
    /**
     * 关注
     * @param User $user 粉丝信息
     * @param int $user_id 被关注id
     * @param bool $trans 是否开启事务
     * @return array
     */
    public static function follow(User $user, int $user_id,bool $trans=true):array
    {
        if ($trans)
            Db::startTrans();
        try {
            if (!User::where('id', $user_id)->where('status','normal')->count()){
                throw new Exception('当前用户不存在或已冻结');
            }
            $mutual = UserFollow::MUTUAL_NO;
            $userInfo = UserFollow::where('user_id',$user->id)->where('fans_id',$user_id)->where('cancel',UserFollow::CANCEL_NO)->find();
            if ($userInfo){
                $mutual = UserFollow::MUTUAL_YES;
            }
            $fansInfo = UserFollow::where('user_id',$user_id)->where('fans_id',$user->id)->find();
            if ($fansInfo){
                if ($fansInfo['cancel'] == UserFollow::CANCEL_NO){
                    throw new Exception('您已关注该用户，无须重复操作');
                }else{
                    UserFollow::where('user_id',$user_id)->where('fans_id',$user->id)->update(['cancel'=>UserFollow::CANCEL_NO,'mutual'=>$mutual]);
                    if ($mutual){
                        UserFollow::where('user_id',$user->id)->where('fans_id',$user_id)->update(['cancel'=>UserFollow::CANCEL_NO,'mutual'=>$mutual]);
                    }

                }
            }else{
                $insert = [
                    'user_id'=>$user_id,
                    'fans_id'=>$user->id,
                    'mutual'=>$mutual,
                    'create_time'=>time(),
                    'update_time'=>time()
                ];

                UserFollow::insert($insert);
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

    /**
     * 取消关注
     * @param User $user
     * @param int $user_id
     * @param bool $trans
     * @return array
     */
    public static function cancel(User $user,int $user_id,bool $trans=true):array
    {
        if ($trans)
            Db::startTrans();
        try {
            $fansInfo = UserFollow::where('user_id',$user_id)->where('fans_id',$user->id)->find();
            if (!$fansInfo){
                throw new Exception('您未关注当前用户，无法取消关注');
            }

            if ($fansInfo['cancel'] == UserFollow::CANCEL_YES){
                throw new Exception('您已取消关注该用户，无须重复操作');
            }else{
                UserFollow::where('user_id',$user_id)->where('fans_id',$user->id)->update(['cancel'=>UserFollow::CANCEL_YES,'mutual'=>UserFollow::MUTUAL_NO,'update_time'=>time()]);
                $userInfo = UserFollow::where('user_id',$user->id)->where('fans_id',$user_id)->where('cancel',UserFollow::CANCEL_NO)->find();
                if ($userInfo){
                    UserFollow::where('user_id',$user->id)->where('fans_id',$user_id)->update(['mutual'=>UserFollow::MUTUAL_NO,'update_time'=>time()]);
                }

            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}